package com.ruipengkj.jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.ruipengkj.ApplicationConfig;
import com.ruipengkj.init.InitContext;

public class ConnectionNetWork {

	private Connection connection=null;
	private String username;//数据库用户名
	private String password;//数据库密码
	private String url;
	private String driverName;
	public static void main(String[] args) {
		new ConnectionNetWork();
	}
	
	public ConnectionNetWork(){
		username=InitContext.getinstance().LoadDataBaseData().getProperty("pool.username");
		password=InitContext.getinstance().LoadDataBaseData().getProperty("pool.password");
		url=InitContext.getinstance().LoadDataBaseData().getProperty("pool.url");
		driverName=InitContext.getinstance().LoadDataBaseData().getProperty("pool.driverClassName");
		try {
			Class.forName(driverName);
			connection=DriverManager.getConnection(url,username,password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
//			e.printStackTrace();
		} catch (Exception e) {
			
		}
	}
	
	public ConnectionNetWork(String username, String password, String url){
		this.username=username;
		this.password=password;
		this.url=url;
		driverName=InitContext.getinstance().LoadDataBaseData().getProperty("pool.driverClassName");
		try {
			Class.forName(driverName);
			connection=DriverManager.getConnection(url,username,password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ConnectionNetWork(String driverClassName,String url, String username,String password)throws Exception{
		Class.forName(driverClassName);
		connection=DriverManager.getConnection(url,username,password);
	}
	
	//是否需要建立试图
	//当存在表时，删除表，返回true， 当不存在表，存在试图时，返回false， 都不存在时，直接返回true
	public boolean hasTable(String tableName) throws Exception {
		ApplicationConfig.INIT_PROGRESS++;
		DatabaseMetaData meta = connection.getMetaData();
		ResultSet rsTables = meta.getTables(null , null, tableName, new String[]{"TABLE"});
		if(rsTables.next()){//存在表时
			insert("drop table " + tableName);//删除表，返回true
			return true;
		}else{//不存在表时
			rsTables = meta.getTables(null , null, tableName, new String[]{"VIEW"});
			if(rsTables.next()){
				return false;
			} else {
				return true;
			}
		}
	}
	
	public boolean insert(String sql) throws Exception {
		PreparedStatement ps=null;
		boolean flag=false;
//		try {
			ps=connection.prepareStatement(sql);
			flag=ps.execute();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		} finally{
//			close();
//		}
		return flag;
	}
	
	public void close(){
		try {
			if(connection!=null&&!connection.isClosed()){
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public Connection getConnection() {
		return connection;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}
}
